package com.dzk.cp.c11x5.index.zhx.yl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.dzk.cp.c11x5.index.BaseC11x5Index;
import com.dzk.cp.c11x5.pojo.R5;
import com.dzk.cp.c11x5.util.C11x5Utils;
import com.dzk.cp.c11x5.util.SpringBeanUtils;
import com.dzk.cp.util.CpUtils;

public abstract class ZHXnYLIndex extends BaseC11x5Index {
	
	private String zhxc;
	
	public ZHXnYLIndex setParams(int code, int num, int unit, String zhxc) {
		super.setParams(code, num, unit);
		this.zhxc = zhxc;
		return this;
	}
	
	@Override
	public Map<Integer, Integer> index() {
		Map<Integer, Integer> resultMap = new LinkedHashMap<Integer, Integer>();
		int n = getLastestNum();
		List<? extends R5> r5s = SpringBeanUtils.getR5Service(code).queryLatest(n);
		int[] zhxArr = CpUtils.split2Ints(zhxc);
		int m = zhxArr.length;
		int i = -1, j = 0, k = 0;
		for (; k < n; k++) {
			R5 r5 = r5s.get(k);
			int[] tmpArr = new int[] {r5.getN1(), r5.getN2(), r5.getN3(), r5.getN4(), r5.getN5()};
			if (C11x5Utils.compare(tmpArr, zhxArr, m)) {
				resultMap.put(++j, k - i - 1);
				i = k;
			}
		}
		resultMap.put(++j, k - 1 - i);
		return resultMap;
	}
	
	public static ZHXnYLIndex getInstance(int code, int num, int unit, String zhxc) {
		int n = (zhxc.length() + 1 ) / 3;
		switch (n) {
		case 1:
			return new ZHX1YLIndex().setParams(code, num, unit, zhxc);
		case 2:
			return new ZHX2YLIndex().setParams(code, num, unit, zhxc);
		case 3:
			return new ZHX3YLIndex().setParams(code, num, unit, zhxc);
//		case 4:
//			return new ZHX4YLIndex().setParams(code, num, unit, zhxc);
//		case 5:
//			return new ZHX5YLIndex().setParams(code, num, unit, zhxc);
		default:
			return null;
		}
	}

}

class ZHX1YLIndex extends ZHXnYLIndex {

	@Override
	protected int getZQ() {
		return 11;
	}

}

class ZHX2YLIndex extends ZHXnYLIndex {

	@Override
	protected int getZQ() {
		return 110;
	}

}

class ZHX3YLIndex extends ZHXnYLIndex {

	@Override
	protected int getZQ() {
		return 990;
	}

}

class ZHX4YLIndex extends ZHXnYLIndex {

	@Override
	protected int getZQ() {
		return 11 * 10 * 9 * 8;
	}

}

class ZHX5YLIndex extends ZHXnYLIndex {

	@Override
	protected int getZQ() {
		return 11 * 10 * 9 * 8 * 7;
	}

}
